home *** CD-ROM | disk | FTP | other *** search
/ Your Choice 3 / Your Choice Software Collection 3.iso / prgmming / swag05 / joystick.swg < prev    next >
Encoding:
Text File  |  1994-09-22  |  5.8 KB  |  1 lines

  1. SWAGOLX.EXE (c) 1993 GDSOFT  ALL RIGHTS RESERVED 00003                                                                           1      05-25-9408:17ALL                      IAN HINSON               Joystick interface       SWAG9405            13     ╔   πUNIT JoyStick;π(* Public Domain.  Written by Ian Hinson   November 1993 *)ππINTERFACEππPROCEDURE ReadPosns;π{ Updates values of JoyA_X, JoyA_Y, JoyB_X, and JoyB_Y }ππPROCEDURE ReadButtons;π{ Updates the state of all buttons }ππ{ Call the function for whichever button(s) you want to testπ  after updating all their states with a call to ReadButtons. }πFUNCTION JoyA_Button1: BOOLEAN;πFUNCTION JoyA_Button2: BOOLEAN;πFUNCTION JoyB_Button1: BOOLEAN;πFUNCTION JoyB_Button2: BOOLEAN;πFUNCTION AnyButton: BOOLEAN;ππVARπ{ These variables provide the X&Y positions afterπ  they have been updated by a call to ReadPositions }πJoyA_X, JoyA_Y, JoyB_X, JoyB_Y: WORD;πππIMPLEMENTATIONππVARπbuttons: SET OF (JoyA_1, JoyA_2, JoyB_1, JoyB_2);ππPROCEDURE ReadPosns; ASSEMBLER;π   ASMπ   mov ah,$84π   mov dx,1π   int $15π   mov JoyA_X,axπ   mov JoyA_Y,bxπ   mov JoyB_X,cxπ   mov JoyB_Y,dxπ   END;ππPROCEDURE ReadButtons; ASSEMBLER;π   ASMπ   mov ah,$84π   mov dx,0π   int $15π   shr al,4π   xor al,$0Fπ   mov buttons,alπ   END;ππFUNCTION JoyA_Button1: BOOLEAN;π  BEGINπ    JoyA_Button1 := JoyA_1 IN buttons;π  END;ππFUNCTION JoyA_Button2: BOOLEAN;π  BEGINπ    JoyA_Button2 := JoyA_2 IN buttons;π  END;ππFUNCTION JoyB_Button1: BOOLEAN;π  BEGINπ    JoyB_Button1 := JoyB_1 IN buttons;π  END;ππFUNCTION JoyB_Button2: BOOLEAN;π  BEGINπ    JoyB_Button2 := JoyB_2 IN buttons;π  END;ππFUNCTION AnyButton: BOOLEAN;π  BEGINπ    AnyButton := buttons <> [];π  END;ππEND.ππ                                                                                           2      05-25-9408:17ALL                      SEAN PALMER              Accessing The Joystick   SWAG9405            19     ╔   {by Sean Palmer}π{public domain}π{feel free to put this in SWAG or whatever}ππunit joy;ππ{unit for accessing joystick 0}ππinterfaceππvarπ installed:boolean; {true if joystick 0 present at unit startup}πvarπ X,Y:word;          {stick position}πvarπ A,B:boolean;       {buttons down?}πconstπ Cal_L:word=$FFFF;   {rect containing calibration extent of 'center'}π Cal_T:word=$FFFF;π Cal_R:word=0;π Cal_B:word=0;ππprocedure sample;   {take a sample of current joystick 0 state}πprocedure swirlCalibrate;πprocedure centerCalibrate;πππimplementationππprocedure sample;assembler;asmπ xor si,si     {x count}π xor di,di     {y count}π mov dx,$201   {Game port}π out dx,al     {Fire the joystick one-shots}π@@L:π in  al,dx     {get joystick bits}π mov ah,al     {save original value}π shr al,1      {joy 0 x expired? 0 if so, else 1}π adc si,0      {accumulate in x}π jc @@TOOLONG  {if overflow, give up}π shr al,1      {joy 0 y expired? 0 if so, else 1}π adc di,0      {accumulate in y}π jc @@TOOLONG  {if overflow, give up}π test ah,3π jnz @@L       {keep going til they're both 0 or we overflow}π not ah        {flip button bits so 1=pressed}π mov al,ahπ and al,$10    {mask off buttons and store them}π mov A,alπ and ah,$20π mov B,ahπ mov X,si      {store x & y coords}π mov Y,diπ jmp @@Xπ@@TOOLONG:π mov X,-1      {overflowed, return -1 as error}π mov Y,-1π mov A,0π mov B,0π@@X:πend;ππprocedure swirlCalibrate;begin  {display message before starting this one!}π repeat sample until not (A or B);{make sure button is up}π repeat                           {collect max extents}π  sample;π  if x<Cal_L then Cal_L:=x;π  if x>Cal_R then Cal_R:=x;π  if y<Cal_T then Cal_T:=y;π  if y>Cal_B then Cal_B:=y;π  until a;                        {until user presses a button}π  Cal_L:=((Cal_L*3)+Cal_R)div 4;      {now adjust for center by}π  Cal_R:=((Cal_R*3)+Cal_L)div 4;      { weighted averaging}π  Cal_T:=((Cal_T*3)+Cal_B)div 4;π  Cal_B:=((Cal_B*3)+Cal_T)div 4;π end;ππprocedure centerCalibrate;var x2,y2:word;begin {doesn't require userπinteraction}π sample;π x2:=x shr 1;π y2:=y shr 1;π Cal_L:=x-x2;π Cal_R:=x+x2;π Cal_T:=y-y2;π Cal_B:=y+y2;π end;ππbeginπ sample;π installed:=(x<>$FFFF);πend.π                                                                                                                          3      05-25-9408:17ALL                      CHRIS WILPER             Joystick Testing         SWAG9405            13     ╔   πProgram JoyTest;ππusesπ   crt;ππvarπ   ch:char;π   x1,y1,x2,y2:word;ππFunction JoyExist:boolean;πvarπ   temp:byte;πbeginπ   asmπ      mov ah,84hπ      mov dx,00hπ      int 15hπ      mov temp,alπ   end;π   if temp=0 then JoyExist:=falseπ   else JoyExist:=true;πend;ππFunction JoyX:word;πvarπ   temp:word;πbeginπ   asmπ      mov ah,84hπ      mov dx,01hπ      int 15hπ      mov temp,axπ   end;π   JoyX:=temp;πend;ππFunction JoyY:word;πvarπ   temp:word;πbeginπ   asmπ      mov ah,84hπ      mov dx,01hπ      int 15hπ      mov temp,bxπ   end;π   JoyY:=temp;πend;ππFunction JoyBtn1:boolean;πvarπ   temp:byte;πbeginπ   asmπ      mov ah,84hπ      mov dx,00hπ      int 15hπ      mov temp,al;π   end;π   if temp and 16 = 16 then JoyBtn1:=falseπ      else JoyBtn1:=true;πend;ππFunction JoyBtn2:boolean;πvarπ   temp:byte;πbeginπ   asmπ      mov ah,84hπ      mov dx,00hπ      int 15hπ      mov temp,al;π   end;π   if temp and 32 = 32 then JoyBtn2:=falseπ      else JoyBtn2:=true;πend;ππProcedure JoyCalibrate;ππbeginππ   writeln('Move Joystick to upper-left, and press a button...');π   repeatπ      x1:=JoyX;π      y1:=JoyY;π   until JoyBtn1 or JoyBtn2;π   repeat until not JoyBtn1 or JoyBtn2;π   writeln('Move Joystick to lower-right, and press a button...');π   repeatπ      x2:=JoyX;π      y2:=JoyY;π   until JoyBtn1 or JoyBtn2;ππend;ππbeginππ   clrscr;π   if not joyexist then beginπ      writeln('No joystick');π      halt;π   end;π   joycalibrate;π   write(#10#13,'Range is from (',x1,',',y1,') to (',x2,',',y2,')');π   ch:=readkey;πend.ππ